package com.cc;

import com.cc.annotation.MeCom;
import com.cc.service.OrderService;
import com.cc.service.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSource;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.scheduling.annotation.EnableAsync;


/**
 * @title: AppConfig
 * @author: lbj
 * @date: 2022/5/4 9:47 下午
 * @description: TODO 类描述
 */

//@Configuration
//@PropertySource("classpath:spring.properties")
//@ComponentScan(value = "com.cc"
//        ,includeFilters = @ComponentScan.Filter(
//                type = FilterType.ANNOTATION
//        , value = MeCom.class)
//)
//@EnableAsync
//@Import(UserService.class)
@ComponentScan("com.cc")
//@ImportResource("classpath:spring.xml")
//@Import(DefaultAdvisorAutoProxyCreator.class)
//@EnableAspectJAutoProxy
//@Configuration
//@Import(UserService.class)
@PropertySource("classpath:db.properties")
public class AppConfig {

    @Bean
    public UserService userService(){
        return new UserService();
    }

//    @Import()
//    @EventListener(ContextRefreshedEvent.class)
//    public void myEvent(ContextRefreshedEvent event){
//        System.out.println("all singleton beans loaded, ContextRefreshedEvent! ");
//    }

//    @EventListener
//    public void listenerTest(ApplicationEvent event){
//        if (event instanceof PayloadApplicationEvent) {
//            PayloadApplicationEvent e = (PayloadApplicationEvent) event;
//            System.out.println(e.getPayload());
//        }
//    }

//    @Bean
//    public MessageSource messageSource(){
//        final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
//        messageSource.setBasename("messages");
//        return messageSource;
//    }

    // 代理方式 1
//    @Bean
//    public ProxyFactoryBean userServiceProxy(){
//        final UserService userService = new UserService();
//
//        final ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
//        proxyFactoryBean.setTarget(userService);
//        proxyFactoryBean.addAdvice(new MethodInterceptor() {
//            @Override
//            public Object invoke(MethodInvocation invocation) throws Throwable {
//                System.out.println("before...");
//                final Object result = invocation.proceed();
//                System.out.println("after...");
//                return result;
//            }
//        });
//        return proxyFactoryBean;
//    }
//
//    // 代理方式 2
//    @Bean
//    public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
//        final BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
//        beanNameAutoProxyCreator.setBeanNames("userSe*");
//        beanNameAutoProxyCreator.setInterceptorNames("meAroundAdvice");
//        beanNameAutoProxyCreator.setProxyTargetClass(true);
//        return beanNameAutoProxyCreator;
//    }
//
//    @Bean
//    public MethodInterceptor meAroundAdvice(){
//        return new MethodInterceptor() {
//            @Override
//            public Object invoke(MethodInvocation invocation) throws Throwable {
//                System.out.println("me before....");
//                final Object proceed = invocation.proceed();
//                System.out.println("me after...");
//                return proceed;
//            }
//        };
//    }


    // 代理方式 3
//    @Bean
//    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
//        final NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
//        pointcut.addMethodName("test");
//
//        final DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
//        defaultPointcutAdvisor.setPointcut(pointcut);
//        defaultPointcutAdvisor.setAdvice(new MethodInterceptor() {
//            @Override
//            public Object invoke(MethodInvocation invocation) throws Throwable {
//                System.out.println("default before....");
//                final Object proceed = invocation.proceed();
//                System.out.println("default after...");
//                return proceed;
//            }
//        });
//        return defaultPointcutAdvisor;
//    }

//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
//        final DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
//        return defaultAdvisorAutoProxyCreator;
//    }


//    @Primary
//    @Bean
//    public OrderService orderService1(){
//        return new OrderService();
//    }
//
//    @Bean
//    public OrderService orderService2(){
//        return new OrderService();
//    }


//    @Bean
//    public UserService userService(){
//        return new UserService();
//    }
//
//    @Bean
//    public OrderService orderService(){
//        System.out.println(userService());
//        System.out.println(userService());
//        return new OrderService();
//    }

//    @PrototypeBean
//    public UserService userService(){
//        return new UserService();
//    }

//    @Bean(autowireCandidate = false)
//    @Bean
//    public OrderService orderService(){
//        return new OrderService();
//    }

//    @Bean(autowireCandidate = false)
//    public OrderService orderService1(){
//        return new OrderService();
//    }

}
